import React, {Component} from 'react';

import './index.less';

import {base, toast} from 'nc-lightapp-front';

import {hrAjax as proFetch} from 'src/hrpub/common/utils/utils';

import language from '../../language';

const {NCDatePicker, NCModal, NCButton} = base;

const {
    Header: ModalHeader,
    Body: ModalBody,
    Footer: ModalFooter
} = NCModal;

class StopModal extends Component {
    constructor(props) {
        super(props);

        this.state = {
            stopDate: ''
        };

        this.renderContent = this.renderContent.bind(this);
        this.changeDate = this.changeDate.bind(this);
        this.stopKeyPerson = this.stopKeyPerson.bind(this);
        this.checkStop = this.checkStop.bind(this);
    }

    checkStop() {   
        const {
            currentSelectDepartment,
            orgVal,
            editTable
        } = this.props;
        let selectRows = editTable.getCheckedRows('ncc60071025list');

        let param_str = [];
        selectRows.map((item) => {
            param_str.push(item.data.values['pk_psnorg'].value);
        });

        let postData = {
            param_str: param_str.join(','),
            pk_keypsn_group: currentSelectDepartment,
            pk_org: orgVal.refpk
        };
        return proFetch({
            url: '/nccloud/hrhi/keypsn/KeyPsnStopCheckAction.do',
            body: postData
        })
    }

    stopKeyPerson() {
        const {
            currentSelectDepartment,
            orgVal,
            editTable,
            modal
        } = this.props;
        let selectRows = editTable.getCheckedRows('ncc60071025list');

        if(!this.state.stopDate) {
            toast({
                color: 'warning',
                content: language['hi6007-000226'] // '结束日期不能未空'
            });
            return false;
        }

        let param_str = [];
        selectRows.map((item) => {
            param_str.push(item.data.values['pk_psnorg'].value);
        });

        let postData = {
            param_str: param_str.join(','),
            pk_keypsn_group: currentSelectDepartment,
            pk_org: orgVal.refpk
        };
        proFetch({
            url: '/nccloud/hrhi/keypsn/KeyPsnStopDoAction.do',
            body: {
                ...postData,
                stopdate: this.state.stopDate
            }
        })
            .then((res) => {
                if(res.success) {
                    toast({
                        color: 'success',
                        content: language['hi6007-000227']// '终止成功'
                    });
                    this.props.updateState({
                        stopModalVisible: false
                    });
                    this.props.getMainTableData();
                }
            });
    }

    changeDate(date) {
        this.setState({
            stopDate: date
        });
    }

    renderContent() {

        const {
            stopDate
        } = this.state;

        return (
            <div
                style={{
                    display: 'flex',
                    justifyContent: 'space-between',
                    // lineHeight: '38px',
                    width: '280px',
                    margin: '0 auto',
                    // paddingTop: '30px',
                    paddingBottom: '30px'
                }}
            >
                <label
                    style={{
                        textAlign: 'right',
                        display: 'inline-block',
                        width: '72px'
                    }}
                >   
                    {language['hi6007-000104']/** 结束日期 */}
                </label>
                <span
                    style={{
                        textAlign: 'left',
                        display: 'inline-block',
                        width: '190px',
                        paddingRight: '30px'
                    }}
                >
                    <NCDatePicker
                        value={stopDate}
                        onChange={this.changeDate}
                    />
                </span>
            </div>
        );
    }

    render() {
        const {
            stopModalVisible
        } = this.props;
        return (
            <NCModal
                show={stopModalVisible}
                onHide={() => {
                    this.setState({
                        stopDate: ''
                    });
                    this.props.updateState({
                        stopModalVisible: false
                    });
                }}
                className='stop-key-man-modal'
            >
                <ModalHeader
                    closeButton={true}
                >
                    {language['hi6007-000224']/** 终止关键人员 */}
                </ModalHeader>
                <ModalBody>
                    {this.renderContent()}
                </ModalBody>
                <ModalFooter>
                    <NCButton
                        onClick={this.stopKeyPerson}
                    >   
                        {language['hi6007-000004']/*确定*/}
                    </NCButton>
                    <NCButton
                        onClick={() => {
                            this.setState({
                                stopDate: ''
                            });
                            this.props.updateState({
                                stopModalVisible: false
                            });
                        }}
                    >
                        {language['hi6007-000005']/*取消*/}
                    </NCButton>
                </ModalFooter>
            </NCModal>
        );
    }
}

export default StopModal